package com.jn.erp.wechat.staff.api.controller;

import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.web.api.course.entity.dto.TCourseDto;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderDto;
import com.jn.erp.web.api.course.entity.vo.DataTrendsVo;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderVo;
import com.jn.erp.web.api.course.entity.vo.TCourseVo;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.course.service.TCourseService;
import com.jn.erp.web.api.student.entity.dto.TStudentCourseDto;
import com.jn.erp.web.api.student.entity.vo.StudentCourseVo;
import com.jn.erp.web.api.student.entity.vo.TStudentCourseVo;
import com.jn.erp.web.api.student.service.StudentCourseService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.core.WxTableDataInfo;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 课程套餐 相关接口管理
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/29 19:52
 */
@RestController
@RequestMapping("/wx/course")
public class MpCourseController extends BaseWxAuthController {
    @Autowired
    private TCourseService tCourseService;

    @Autowired
    private StudentCourseService studentCourseService;

    @Autowired
    private TCourseOrderService courseOrderService;


    //-------------------------------------课程---------------------
    /**
     课程列表    /tCourse/list
     */
    @GetMapping("list")
    public WxTableDataInfo list(TCourseVo tCourseVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseVo.setCreateUser(String.valueOf(getUserId()) );
        tCourseVo.setPOrgId(getPOrgId());
        startPage();
        List<TCourseDto> list = tCourseService.courseList(tCourseVo);
        return getWxDataTable(list);
    }

    //-------------------------------------新建课程---------------------
    /**
     课程新建    /tCourse/save
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody TCourseVo tCourseVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        String code = tCourseService.saveCourse(tCourseVo);
        return AjaxResult.success(code);
    }

    //-------------------------------------课程详情---------------------
    /**
     课程修改    /tCourse/update
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody TCourseVo tCourseVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        String code = tCourseService.updateCourse(tCourseVo);
        return AjaxResult.success(code);
    }

    /**
     课程删除   /tCourse/delete
     */
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody TCourseVo tCourseVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        tCourseVo.setIsDelete("1");
        Boolean boo = tCourseService.deleteCourse(tCourseVo);
        return AjaxResult.success(boo);
    }

    //------------------------报名缴费---------------------------------------

    /**
     * 学员报名  /tStudentCourse/application
     */
    @PostMapping("/student/application")
    public AjaxResult application(@RequestBody StudentCourseVo studentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        studentCourseVo.setOrgId(wxUser.getOrgIdLong() );
        //当前登录人id
        studentCourseVo.setCreator(wxUser.getUserId());
        String code = studentCourseService.application(studentCourseVo,true);
        return AjaxResult.success(code);
    }



    // ------------------学员明细信息---------------------------------
    /**
     * 学员内报读课程   /tStudentCourse/listSecond
     */
    @PostMapping("/listSecond")
    public AjaxResult listSecond(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong() );
        //当前登录人id
        tStudentCourseVo.setEditor(wxUser.getUserId());
        tStudentCourseVo.setEditTime(LocalDateTime.now());
        List<TStudentCourseDto> list = studentCourseService.listSecond(tStudentCourseVo);
        return AjaxResult.success(list);
    }




    //---------------------------学员报名续费-----------------------------


    /**
     * 停课，复课  /tStudentCourse/stopsAndResume
     */
    @PostMapping("/stopsAndResume")
    public AjaxResult stopsAndResume(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentCourseVo.setEditor(wxUser.getUserId());
        tStudentCourseVo.setEditTime(LocalDateTime.now());

        studentCourseService.stopsAndResume(tStudentCourseVo);
        return AjaxResult.success("S200");
    }



    /**
     * 结课     /tStudentCourse/classEnding
     */
    @PostMapping("/classEnding")
    public AjaxResult classEnding(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentCourseVo.setEditor(wxUser.getUserId());
        tStudentCourseVo.setEditTime(LocalDateTime.now());
        studentCourseService.classEnding(tStudentCourseVo);
        return AjaxResult.success("S200");
    }




    /**
     * 清零   /tStudentCourse/update
     */
    @PostMapping("/studentCourseUpdate")
    public AjaxResult update(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentCourseVo.setEditor(wxUser.getUserId());
        tStudentCourseVo.setEditTime(LocalDateTime.now());

        studentCourseService.updateStudentCourse(tStudentCourseVo);
        return AjaxResult.success("S200");
    }




    /**
     *超上处理   /tStudentCourse/dealExceed
     */
    @PostMapping("/studentCourseDealExceed")
    public AjaxResult dealExceed(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentCourseVo.setEditor(wxUser.getUserId());
        tStudentCourseVo.setEditTime(LocalDateTime.now());

        String code = studentCourseService.dealExceed(tStudentCourseVo);
        return AjaxResult.success(code);
    }


    /**
     * 订单详情   /tStudentCourse/detail
     */
    @PostMapping("/studentCourseDetail")
    public AjaxResult detail(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentCourseVo.setCreator(wxUser.getUserId());
        TStudentCourseDto tStudentCourseDto = studentCourseService.studentCourseDetail(tStudentCourseVo);
        return AjaxResult.success(tStudentCourseDto);
    }

    //------------------------------------试听列表-------------------------------
    /**
     * 课次列表   /tCourseOrder/orderFilterList
     *
     */
    @GetMapping("/orderFilterList")
    public WxTableDataInfo orderFilterList(TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();

        //机构id
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        startPage();
        List<TCourseOrderDto> list = courseOrderService.orderFilterList(tCourseOrderVo);
        return getWxDataTable(list);
    }

    //-------------------------------------消课记录---------------------------------

    /**
     * 课消列表  /tCourseOrder/orderListThird
     * */
    @GetMapping("/orderListThird")
    public WxTableDataInfo orderListThird(TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        startPage();
        List<TCourseOrderDto> tCourseOrderDtos= courseOrderService.orderListThird(tCourseOrderVo);
        return getWxDataTable(tCourseOrderDtos);
    }


    /**
     * 课消详情金额    /tCourseOrder/orderCountSecond
     * */
    @PostMapping("/orderCountSecond")
    public AjaxResult orderCountSecond(@RequestBody TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();

        //机构id
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        Map map = courseOrderService.orderCountSecond(tCourseOrderVo);
        return AjaxResult.success(map);
    }

    /**
     * 课消详情   /tCourseOrder/orderDetailSecond
     * */
    @PostMapping("/orderDetailSecond")
    public AjaxResult orderDetailSecond(@RequestBody TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();

        //机构id
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        TCourseOrderDto tCourseOrderDto= courseOrderService.orderDetailSecond(tCourseOrderVo);
        return AjaxResult.success(tCourseOrderDto);
    }

//--------------------------------------------财务订单--------------------------------

    /**
     * 实际总收入   /tStudentCourse/sumAmount
     */
    @PostMapping("/studentCourseSumAmount")
    public AjaxResult sum(@RequestBody TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentCourseVo.setEditor(wxUser.getUserId());
        tStudentCourseVo.setEditTime(LocalDateTime.now());

        BigDecimal bigDecimal = studentCourseService.sumAmount(tStudentCourseVo);
        return AjaxResult.success(bigDecimal);
    }


    /**
     * 订单列表   /tStudentCourse/list
     */
    @GetMapping("/studentCourseList")
    public WxTableDataInfo list(TStudentCourseVo tStudentCourseVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentCourseVo.setOrgId(wxUser.getOrgIdLong());
        startPage();
        List<TStudentCourseDto> list = studentCourseService.studentCourseList(tStudentCourseVo);
        return getWxDataTable(list);
    }

    /**
     * 教务数据-核心数据   /tCourseOrder/countAcademicData
     * */
    @PostMapping("/courseOrderCountAcademicData")
    public AjaxResult countAcademicData(@RequestBody DataTrendsVo dataTrendsVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        dataTrendsVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        dataTrendsVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        Map map = courseOrderService.countAcademicData(dataTrendsVo);
        return AjaxResult.success(map);
    }



    /**
     * 财务数据-核心数据  /tStudentCourse/countAcademicData
     * */
    @PostMapping("/studentCourseCountAcademicData")
    public AjaxResult studentCourseCountAcademicData(@RequestBody DataTrendsVo dataTrendsVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        dataTrendsVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        dataTrendsVo.setCreator(wxUser.getUserId() );
        Map map = studentCourseService.countAcademicData(dataTrendsVo);
        return AjaxResult.success(map);
    }
}
